<div class="am-cf am-padding-xs am-padding-bottom-0">
	<div class="am-fl am-cf">
		<strong class="am-text-primary am-text-lg">redis管理工具</strong> / <small>客户端</small>
	</div>
	<div class="am-fr am-cf">
		<button type="button" class="am-btn am-btn-xs am-btn-primary" id="refreshBtn">
			<i class="am-icon-refresh"></i>&nbsp;刷新
		</button>
	</div>
</div>
<hr class="am-margin-xs"/>
<div class="am-scrollable-horizontal">
	<table class="am-table am-table-bordered am-table-striped am-table-hover am-table-compact am-text-nowrap am-table-centered am-margin-bottom-xs">
		<thead>
			<tr id="tableHead"></tr>
			<tr class="am-hide" id="tableTmpl"></tr>
		</thead>
		<tbody id="tableBody"></tbody>
	</table>
</div>
<script>
(function(){
	var redisClientMapping = {
		"addressPort": '客户端（ip:port）',
		'age': '连接时长（秒）',
		'patternSubscrbtions': '订阅的模式数量',
		'channelSubscribtions': '订阅的频道数量'
	}
	$('#refreshBtn').on('click', function(){
		$.ajax({
			url: basePath + 'redis/clients',
			type: 'get',
			dataType: 'json',
			success: function(data){
				$('#tableTmpl').empty();
				$('#tableHead').empty();
				//按照连接时长排序
				data.clients.sort(function(a, b){
					return a.age - b.age;
				});
				for(var key in data.clients[0]){
					$('#tableHead').append('<th>' + ($.getjson(redisClientMapping, key) || key) + "</th>");
					$('#tableTmpl').append('<td>{{' + key + "}}</td>");
				}
				$('#tableBody').empty();
				for(var i=0; i<data.clients.length; i++){
					var tmpl = $.format($('#tableTmpl').html(), data.clients[i]);
					$('#tableBody').append('<tr>' + tmpl + '<tr>');
				}
			}
		});
	});
	$(function(){
		$('#refreshBtn').trigger('click');
	});
})();
</script>
</body>
</html>
